const crypto=require('crypto');
const _=require('underscore');
const config=require('./config');
const passport = require('koa-passport')
const {User}=require('../modles/index');

passport.serializeUser(function(user, done) {
    // console.log('serializeUser>user:',user);
    done(null,user);
});

passport.deserializeUser(async function(user, done) {
    // console.log('deserializeUser>user:',user);
    done(null, user);
});

const LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy(async (username,password,done)=>{
    let user=await User.findOne({username});
    if(!user || user.password!==crypto.createHash('md5').update([password,config.secret].join()).digest('hex'))return done(null,false,'用户名或密码错误');
    done(null,_.pick(user,'_id','username','regTimeStr','avatar'));
}));


const GithubStrategy = require('passport-github').Strategy;
passport.use(new GithubStrategy({
        clientID:config.github.clientID,
        clientSecret:config.github.clientSecret,
        callbackURL:config.github.callbackURL
    },
    (token, tokenSecret, profile, done)=>{
/*        console.log('GithubStrategy callback>args>token:',token);
        console.log('GithubStrategy callback>args>tokenSecret',tokenSecret);
        console.log('GithubStrategy callback>args>profile:', profile);*/
        done(null,profile,token);
    }
));